.toolbar-content
  .meta-toolbar.toolbar.custom-meta-toolbar
    %span.list-pagination{data:{list_ref: "items-list"}}
      - limit = 20
      - current_page = @draft_entries.current_page
      - pages_count = @draft_entries.total_pages
      - count = @draft_entries_count
      - x = limit * (current_page - 1) + 1
      - y = (current_page < pages_count) ? current_page * limit : count
      %span.status= 'list.pagination.x_to_y_of_total'.t(x: x, y: y, total: count)
      %span.paginator
        %a{href: list_backend_draft_journal_path(page: current_page - 1, journal_id: @journal_id, from: @current_from_date.strftime("%F"), to: @current_to_date.strftime("%F"), redirection: @redirection), class: [("btn previous-page"),("disabled" if current_page == 1)], data: {remote: :true}}
          %i
        %a{href: list_backend_draft_journal_path(page: current_page + 1, journal_id: @journal_id, from: @current_from_date.strftime("%F"), to: @current_to_date.strftime("%F"), redirection: @redirection), class: [("btn next-page"),("disabled" if current_page == @draft_entries.total_pages)], data: {remote: :true}}
          %i

  .main-toolbar.toolbar.custom-main-toolbar
    = form_tag list_backend_draft_journal_path, method: :get, remote: true, class: 'filter-form' do
      = label_tag :filter_on.tl
      = select_tag :journal_id, options_from_collection_for_select(Journal.all, :id, :name, @journal_id == 'All' ? nil : @journal_id), prompt: :any_journal.tl
      = label_tag :from, :from.tl
      = date_field_tag :from, @current_from_date.strftime("%F"), class: 'no-arrow-date-input'
      = label_tag :to, :until.tl
      = date_field_tag :to, @current_to_date.strftime("%F"), class: 'no-arrow-date-input'
      = hidden_field_tag :redirection, @redirection
      = button_tag type: 'submit', class: 'btn btn-default' do
        <i class='icon icon-search'></i> #{:filter.tl}

%table
  %thead
    - currency = Preference[:currency]
    %th.act
      %i.icon.icon-check
    %th.act
      %i.icon.icon-edit
    %th.act
      %i.icon.icon-destroy
    %th.med= Journal.name
    %th.sm= JournalEntryItem.human_attribute_name(:entry_number)
    %th.sm= JournalEntryItem.human_attribute_name(:printed_on)
    %th= JournalEntryItem.human_attribute_name(:account)
    %th= JournalEntryItem.human_attribute_name(:name)
    %th.med= JournalEntryItem.human_attribute_name(:variant)
    %th.med= JournalEntry.human_attribute_name(:reference_number)
    %th.right-align.sm= JournalEntryItem.human_attribute_name(:debit)
    %th.right-align.sm= JournalEntryItem.human_attribute_name(:credit)
  %tbody.first-item
    - @draft_entries.each do |entry|
      - entry.items.each do |item|
        - first_item = (entry.items.first === item)
        %tr{class: [("first-item" if first_item),('error' unless entry.balanced?)] }
          %td.act
            - if entry.balanced? && first_item
              %i.icon.icon-check{ style: "color: #688ED8;" }
          %td.act= link_to("<i class='icon icon-edit'></i>".html_safe, edit_backend_journal_entry_path(entry), class: "edit") if first_item
          %td.act= link_to("<i class='icon icon-destroy'></i>".html_safe, backend_journal_entry_path(entry), method: :delete, class: "destroy") if first_item
          %td.med= link_to(entry.journal.name, { controller: :journals, action: :show, id: entry.journal.id} ) if first_item
          %td.sm= link_to(entry.number, { controller: :journal_entry_items, action: :show, id: item.id} ) if first_item
          %td.sm= entry.printed_on.strftime("%d/%m/%Y") if first_item
          %td= link_to(item.account_label, { controller: :accounts, action: :show, id: item.account.id})
          %td= truncate(item.name, length: 50)
          %td.med= item.variant&.name
          %td.med= entry.reference_number
          %td.right-align.sm= number_to_accountancy(item.debit, currency, false)
          %td.right-align.sm= number_to_accountancy(item.credit, currency, false)

- unless @draft_entries_count < 1
  .footer-btn-container
    %button{ class: [('btn btn-default no-icn'),('disabled' if @unbalanced_entries_count > 0)], type: 'button', 'data-toggle': 'modal', 'data-target': '#entries-validation-modal' }= t("labels.validate_x_entries", count: @draft_entries_count)


.modal.fade#entries-validation-modal{ tabindex: '-1', role: 'dialog' }
  .modal-dialog{ role: 'document' }
    .modal-content
      .modal-header
        %h4.modal-title= :confirmation.tl
        %button.close{ type: 'button', 'data-dismiss': 'modal', 'aria-label': 'Close' }
          %span{ 'aria-hidden': 'true' }
            %i.icon.icon-destroy
      .modal-body
        = :journal_entries_deletion_confirmation_message.tl
      .modal-footer
        %button.btn.btn-default.no-icn{ type: 'button', 'data-dismiss': 'modal' }= :cancel.tl
        = link_to t("labels.validate_x_entries", count: @draft_entries_count), { controller: :draft_journals, action: :confirm_all, from: @current_from_date, to: @current_to_date, journal_id: @journal_id, redirection: params[:redirection] }, { class: 'btn btn-primary no-icn', method: :post}
